//
//  FeedBackViewController.m
//  LoanOne
//
//  Created by WangZhenyu on 2017/10/18.
//  Copyright © 2017年 WangZhenyu. All rights reserved.
//

#import "FeedBackViewController.h"
#import "GlobalFunction.h"

@interface FeedBackViewController ()<UIGestureRecognizerDelegate>{
    BOOL        editBool;
}

@property (weak, nonatomic) IBOutlet UITextView *contentView;
@property (weak, nonatomic) IBOutlet UITextField *additionalTextField;
@property (weak, nonatomic) IBOutlet UIView *bgView;

@end

@implementation FeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"意见反馈";
    if (self.foraml) {
        [self addBackBtn];
        [((UIButton *)[self addRigthBtn:@"提交" withColor:nil]) addTarget:self action:@selector(sendPress) forControlEvents:UIControlEventTouchUpInside];
    }else{
        [self addBlackBackBtn];
        [((UIButton *)[self addRigthBtn:@"提交" withColor:ThemeColor]) addTarget:self action:@selector(sendPress) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
    editBool = YES;
    
    self.contentView.textColor = RGBACOLOR(0, 0, 0.0980392, 0.22);
    
    [self.bgView setFrame:CGRectMake(0, 64 + IS_iPhoneX_Top, SCREEN_WIDTH_DEVICE, 273)];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc{
    [[ServiceRequest sharedService] cancelDataTaskForKey:@"feedback"];
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    if (editBool) {
        editBool = NO;
        textView.text = @"";
        [textView setTextColor:RGB(45, 45, 45)];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    if (textView.text.length == 0) {
        editBool = YES;
        textView.text = @"请输入您的反馈信息，我们将为您不断改进。（500字以内）";
        [textView setTextColor:RGBACOLOR(0, 0, 0.0980392, 0.22)];
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSInteger textViewLength = [textView.text stringByReplacingCharactersInRange:range withString:text].length;
    if (textViewLength > 500) {
        return NO;
    }else if (textViewLength == 0){
        return YES;
    }else if (textViewLength > 0){
        return YES;
    }else{
        return YES;
    }
}

- (void)textViewDidChange:(UITextView *)textView{
    [textView setTextColor:RGBACOLOR(45, 45, 45, 1)];
}

- (void)sendPress {
    if (self.contentView.text.length == 0 || [self.contentView.text isEqualToString:@"请输入您的反馈信息，我们将为您不断改进。（500字以内）"]) {
        [self showHUDAlert:@"请输入内容"];
        return;
    }

    [self loadingHUDAlert:@"正在发送"];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];

    [dic setObject:self.contentView.text forKey:@"content"];
    [dic setObject:self.additionalTextField.text forKey:@"title"];
    
    [[ServiceRequest sharedService] POSTJSON:@"feedback" parameters:dic success:^(id responseObject) {
        [self hideHudAlert];
        
        [self showHUDAlert:@"非常感谢您的反馈！"];
        [self performSelector:@selector(goBackAction) withObject:self afterDelay:2.0f];
        
        [self.view endEditing:YES];
    } failure:^(NSString *error, NSInteger code) {
        [self hideHudAlert];
        [self showHUDAlert:error];
    }];
}
@end
